/* eslint-disable */
const withTypescript = require('@zeit/next-typescript')
const withLess = require('@zeit/next-less')
const withBundleAnalyzer = require("@zeit/next-bundle-analyzer");
const lessToJS = require('less-vars-to-js')
const fs = require('fs')
const path = require('path')

// Where your antd-custom.less file lives
const themeVariables = lessToJS(
  fs.readFileSync(path.resolve(__dirname, './assets/antd-custom.less'), 'utf8')
)

// fix: prevents error when .less files are required by node
if (typeof require !== 'undefined') {
  require.extensions['.less'] = file => {}
}

const isProd = process.env.NODE_ENV === 'production'

module.exports = withBundleAnalyzer(withTypescript(withLess({
  assetPrefix: isProd ? 'https://owmod-cdn.sylibs.com' : '',
  analyzeServer: ["server", "both"].includes(process.env.BUNDLE_ANALYZE),
  analyzeBrowser: ["browser", "both"].includes(process.env.BUNDLE_ANALYZE),
  bundleAnalyzerConfig: {
    server: {
      analyzerMode: 'static',
      reportFilename: '../bundles/server.html'
    },
    browser: {
      analyzerMode: 'static',
      reportFilename: '../bundles/client.html'
    }
  },
  lessLoaderOptions: {
    javascriptEnabled: true,
    modifyVars: themeVariables, // make your antd custom effective
  },
  webpack(config, options) {
    config.resolve.alias['@components'] = path.join(__dirname, 'components')
    config.resolve.alias['@layouts'] = path.join(__dirname, 'layouts')
    config.resolve.alias['@store'] = path.join(__dirname, 'store')
	config.resolve.alias['@lib'] = path.join(__dirname, 'lib')
	config.resolve.alias['@ant-design/icons/lib/dist$'] = path.resolve(__dirname, 'lib/icon.js');
    if (config.optimization.splitChunks && config.optimization.splitChunks.cacheGroups) {
      if (config.optimization.splitChunks.cacheGroups.commons) {
        const commons = config.optimization.splitChunks.cacheGroups.commons;
        if (commons.minChunks >= 5) {
          commons.minChunks = parseInt(commons.minChunks / 2)
        }
      }
      // Add antd to common
      config.optimization.splitChunks.cacheGroups.antd = {
        name: 'commons',
        chunks: 'all',
        test: /[\\\/]node_modules[\\\/]antd[\\\/]/
      };
    }
    return config
  }
})))